<template>
  <div class="com-page">
    <el-card class="box-card">
      <!-- Layout 布局  栅栏布局  -->
      <el-row :gutter="6">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="param.query"
            suffix-icon="el-icon-search"
            @keyup.enter.native="getData"
            clearable
            @clear="clearInput"
          >
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="info" @click="getData">搜索</el-button></el-col
        >
        <el-col :span="2">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          ></el-col
        >
      </el-row>
      <!-- 用户列表表格开始 -->
      <template>
        <el-table :data="userlist" style="width: 100%" border stripe>
          <el-table-column type="index" width="70" label="#"> </el-table-column>
          <el-table-column prop="username" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="email" label="邮箱"> </el-table-column>
          <el-table-column prop="mobile" label="电话"> </el-table-column>
          <el-table-column prop="role_name" label="角色"> </el-table-column>
          <el-table-column prop="mg_state" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.mg_state"
                @change="userStateChanged(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <!-- 作用域插槽 -->
            <template slot-scope="scope">
              <!-- 修改按钮 -->
              <el-button size="mini" @click="showeditDialog(scope.row.id)"
                >Edit</el-button
              >
              <!-- 删除按钮 -->
              <el-button
                size="mini"
                type="danger"
                @click="opendelete(scope.row.id)"
                >Delete</el-button
              >
              <!-- 分配角色按钮 -->
              <el-button
                size="mini"
                type="warning"
                @click="allotRole(scope.row)"
                >分配角色</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页部分开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="param.pagenum"
        :page-sizes="[1, 5, 8, 10]"
        :page-size="param.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 增加用户对话框 -->
    <el-dialog
      title="添加用户"
      :visible="addDialogVisible"
      :before-close="handleClose"
      width="30%"
      @close="addDialogClose"
    >
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForemRef"
        label-width="100px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email" type="email"></el-input>
        </el-form-item>
        <!-- 手机 -->
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adduser">添加</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog
      title="编辑用户"
      :visible="editDialogVisible"
      :before-close="handleCloseEdit"
      width="30%"
      @close="editDialogClose"
    >
      <!-- 对话框内容 -->

      <el-form
        ref="editForemRef"
        label-width="100px"
        :rules="rules"
        :model="editForm"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input disabled v-model="editForm.username"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" type="email"></el-input>
        </el-form-item>
        <!-- 手机 -->
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edituser">修改</el-button>
      </span>
    </el-dialog>
    <AllotRole
      :dialogVisible="allotDialogVisible"
      @handleCloseDialog="handleCloseDialog"
      ref="AllotRoleRef"
    ></AllotRole>
  </div>
</template>

<script>
import { request } from "@/utils/axios.js";
import AllotRole from "@/components/allotRole";
export default {
  components: { AllotRole },
  props: {},
  data() {
    return {
      param: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      userlist: [],
      addDialogVisible: false,
      addForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
      editDialogVisible: false,
      editForm: {},
      // 控制分配角色对话框的显示和隐藏
      allotDialogVisible: false,
    };
  },
  watch: {},
  computed: {},
  methods: {
    async getData() {
      const { data: ret } = await request.get("users", { params: this.param });
      this.userlist = ret.data.users;
      this.total = ret.data.total;
    },
    handleCurrentChange(s) {
      this.param.pagenum = s;
      this.getData();
    },
    handleSizeChange(v) {
      this.param.pagesize = v;
      this.getData();
    },
    async userStateChanged(userinfo) {
      await request.put(`users/${userinfo.id}/state/${userinfo.mg_state}`);
    },
    clearInput() {
      this.getData();
    },
    addDialogClose() {
      this.$refs.addForemRef.resetFields();
    },
    adduser() {
      this.$refs.addForemRef.validate(async (s) => {
        if (!s) {
          return this.$message.error("请输入正确的数据");
        }
        await request.post("users", this.addForm);
        this.addDialogVisible = false;
      });
    },
    handleClose() {
      this.addDialogVisible = false;
    },
    // 编辑按钮
    async showeditDialog(id) {
      this.editDialogVisible = true;
      const { data: ret } = await request.get("users/" + id);
      this.editForm = ret.data;
    },
    // 修改用户信息 发起请求
    edituser() {
      this.$refs.editForemRef.validate(async (s) => {
        if (!s) {
          return this.$message.error("请输入正确的数据");
        }
        await request.put("users/" + this.editForm.id, this.editForm);
        this.editDialogVisible = false;
        this.getData();
      });
    },
    handleCloseEdit() {
      this.editDialogVisible = false;
    },
    editDialogClose() {
      this.$refs.editForemRef.resetFields();
    },
    opendelete(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await request.delete("users/" + id);
          this.getData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分配角色按钮事件绑定
    async allotRole(v) {
      this.$refs.AllotRoleRef.username = v.username;
      this.$refs.AllotRoleRef.userId = v.id;
      this.$refs.AllotRoleRef.rolename = v.role_name;
      this.$refs.AllotRoleRef.selectvalue = v.role_name;
      const { data: ret } = await request.get("roles");
      this.$refs.AllotRoleRef.roleList = ret.data;
      this.allotDialogVisible = true;
    },
    handleCloseDialog() {
      this.allotDialogVisible = false;
    },
  },
  created() {
    this.getData();
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
.el-row {
  vertical-align: top;
  margin-bottom: 20px;
}
.el-table {
  margin-bottom: 20px;
}
</style>
